package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.pojo.Member;
import com.atguigu.service.OrderSrevice;
import com.atguigu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/Order")
public class OrderController {

    @Reference
    OrderSrevice orderSrevice;

    @Autowired
    JedisPool jedisPool;

    @PostMapping("/submit")
    public Result submit(@RequestBody Map map){
        try {
            String validateCode = (String)map.get("validateCode");
            String telephone = (String)map.get("telephone");
            String redisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
            System.out.println("redisCode = " + redisCode);
            System.out.println("validateCode = " + validateCode);
            System.out.println(telephone + RedisMessageConstant.SENDTYPE_ORDER);

            if (redisCode==null || !validateCode.equals(redisCode)){
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }

            Result result = orderSrevice.saveOrder(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FULL);
        }
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map = orderSrevice.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
